import xml.etree.ElementTree as ET
import os
import json

# voc2007+2012
# voc_clses = [
#             'person' ,
#             'aeroplane' ,
#             'tvmonitor' ,
#             'train',
#             'boat',
#             'dog',
#             'chair',
#             'bird',
#             'bicycle',
#             'bottle',
#             'sheep',
#             'diningtable',
#             'horse',
#             'motorbike',
#             'sofa',
#             'cow',
#             'car',
#             'cat',
#             'bus',
#             'pottedplant'
# ]
voc_clses = ['plane', 'head', 'tail']

voc_xmls = './all/annotations'
json_name = './instances_train2017.json'

dict_classes = dict(zip(voc_clses, range(len(voc_clses))))

categories = []
for iind, cat in enumerate(dict_classes):
    cate = {}
    cate['supercategory'] = cat
    cate['name'] = cat
    cate['id'] = iind
    categories.append(cate)

def getimages(xmlname, id):
    sig_xml_box = []
    tree = ET.parse(xmlname)
    root = tree.getroot()
    images = {}
    for i in root:  # 遍历一级节点
        if i.tag == 'filename':
            file_name = i.text  # 0001.jpg
            # print('image name: ', file_name)
            if len(file_name.strip().split('.')) == 1:  # 没有尾号
                images['file_name'] = file_name + '.jpg'
            else:
                images['file_name'] = file_name
        if i.tag == 'size':
            for j in i:
                if j.tag == 'width':
                    width = j.text
                    images['width'] = width
                if j.tag == 'height':
                    height = j.text
                    images['height'] = height
        if i.tag == 'object':
            for j in i:
                if j.tag == 'name':
                    cls_name = j.text
                    cat_id = dict_classes[cls_name]
                if j.tag == 'bndbox':
                    bbox = []
                    xmin = 0
                    ymin = 0
                    xmax = 0
                    ymax = 0
                    for r in j:
                        if r.tag == 'xmin':
                            xmin = eval(r.text)
                        if r.tag == 'ymin':
                            ymin = eval(r.text)
                        if r.tag == 'xmax':
                            xmax = eval(r.text)
                        if r.tag == 'ymax':
                            ymax = eval(r.text)
                    bbox.append(xmin)
                    bbox.append(ymin)
                    bbox.append(xmax - xmin)
                    bbox.append(ymax - ymin)
                    bbox.append(id)   # 保存当前box对应的image_id
                    bbox.append(cat_id)
                    # anno area
                    bbox.append((xmax - xmin) * (ymax - ymin) )   # bbox的ares
                    # coco中的ares数值是 < w*h 的, 因为它其实是按segmentation的面积算的,所以我-10.0一下...
                    sig_xml_box.append(bbox)
                    # print('bbox', xmin, ymin, xmax - xmin, ymax - ymin, 'id', id, 'cls_id', cat_id)
    images['id'] = id
    # print ('sig_img_box', sig_xml_box)
    return images, sig_xml_box



xml_names = []

xml_names = os.listdir(voc_xmls)


# 没有txt 根据总量生成数依次读取
#for line in range(2700):
# for line in range(2700,3000):



    # for f in os.listdir(voc_xmls):
    #     if not f.endswith('.xml'):
    #         continue
    #
    #     num_img += 1
    #     xml_file = os.path.join(xml_path, f)
    #
    #
    # name = str(line + 1).zfill(4)  # 0001 0002
    # # print(name)  # name is in str type
    # xml_names.append('trainimg' + name)

xmls = []
bboxes = []
ann_js = {}
for ind, xml_name in enumerate(xml_names):
    xmls.append(os.path.join(voc_xmls, xml_name ))

images = []
for i_index, xml_file in enumerate(xmls):
    image, sig_xml_bbox = getimages(xml_file, i_index)
    images.append(image)
    bboxes.extend(sig_xml_bbox)
ann_js['images'] = images
ann_js['categories'] = categories
annotations = []
for box_ind, box in enumerate(bboxes):
    anno = {}
    anno['image_id'] =  box[-3]
    anno['category_id'] = box[-2]
    anno['bbox'] = box[:-3]
    anno['id'] = box_ind
    anno['area'] = box[-1]
    anno['iscrowd'] = 0
    annotations.append(anno)
ann_js['annotations'] = annotations
json.dump(ann_js, open(json_name, 'w'), indent=4)  # indent=4 更加美观显示